<?php

declare(strict_types=1);

namespace App\Controller\Home\Drp;

use App\Controller\AbstractController;
use App\Model\Device;
use App\Model\Distributors;
use App\Model\Normal;
use App\Model\Station;
use App\Model\StationClass;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;

class HomeStationClassController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Station $station;


    #[Inject]
    protected Distributors $distributors;

    #[Inject]
    protected Device $device;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**创建编辑
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
   public function create(RequestInterface $request): ResponseInterface{
       $info = $this->distributors->getInfo($request);
       if(!$info){
           return $this->error(code:'400',msg:'登录态失效 请重新登录');
       }
        $name = $request->input("name");
        $sort = $request->input("sort",0);
        $id = $request->input("id",0);
        if($id){
            $model = StationClass::find($id);
        }else{
            $model = new StationClass();
        }
        $model->distributor_id = $info['id'];
        $model->name = $name;
        $model->sort = $sort;
        $model->save();
        return $this->success();
   }

    /**划拨
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
   public function batch(RequestInterface $request): ResponseInterface{
       $stations = $request->input("station");
       $id = $request->input('id');
       if(!is_array($stations)){
           $stations = json_decode($stations,true);
       }
       $class = StationClass::find($id);
       if(!$class){
           return $this->error(1001,'分组不存在');
       }
       Station::whereIn('id',$stations)->update(['class_id'=>$id]);
       return $this->success();
   }

    /**删除分组
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
   public function delete(RequestInterface $request): ResponseInterface{
       $id = $request->input('id');
       if(!$id){
           return $this->error('1001','缺少参数id');
       }
       $model = StationClass::find($id);
       if(!$model){
           return $this->error(1001,'分组不存在');
       }
       try{
            $model->delete();
            Station::where('class_id',$id)->update(['class_id'=>0]);
            return $this->success();
       }catch (\Exception $e){
           return $this->error(1002,$e->getMessage());
       }
   }

    /**获取单个详情
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
   public function getOne(RequestInterface $request){
       $id = $request->input('id');
       if(!$id){
           return $this->error(1001,'分组不存在');
       }
       $model = StationClass::where('id',$id)->with('stations')->first();
       return $this->success($model);
   }

    /**移除电站
     * @param RequestInterface $request
     * @return ResponseInterface|void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
   public function clearClass(RequestInterface $request){
       $station_id = $request->input('station_id');
       if(!$station_id){
           return $this->error(1001,'缺少参数电站id');
       }
       Station::where('id',$station_id)->update(['class_id'=>0]);
       return $this->success();
   }

    /**获取分组列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
   public function getList(RequestInterface $request){
       $info = $this->distributors->getInfo($request);
       if(!$info){
           return $this->error(code:'400',msg:'登录态失效 请重新登录');
       }
       $list = StationClass::where('distributor_id', $info['id'])
           ->with(['stations' => function ($query) {
               $query->withCount('device') // 关键点
               ->orderByDesc('device_count');
           }])
           ->get();
       return $this->success($list);
   }

}
